package com.chaoyong.higo.adapter;

import java.util.List;

import com.chaoyong.higo.R;
import com.chaoyong.higo.bean.LimShopBean.DataEntity;
import com.chaoyong.higo.utils.TransferTool;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.utils.Values;
import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LimShopAdapter extends RecyclerView.Adapter<LimShopAdapter.MyViewHoler> {
	
	private Context context;
	private LayoutInflater inflater;
	private List<DataEntity> list;
	private SetHolderListener holderListener;

	public LimShopAdapter(Context context, List<DataEntity> list) {
		super();
		this.list = list;
		this.context = context;
		inflater = inflater.from(context);
	}

	class MyViewHoler extends ViewHolder {
		private ImageView image_img;
		private TextView count_tx, left_tx, total_tx;
		private TextView price_tx;
		private ProgressBar count_Pro;
		private ImageButton shop_imgbt, cart_imgbt;

		public MyViewHoler(View arg0) {
			super(arg0);
			// TODO Auto-generated constructor stub
			image_img = (ImageView) arg0.findViewById(R.id.itemhome_image_img);
			price_tx = (TextView) arg0.findViewById(R.id.itemhome_price_tx);
			count_Pro = (ProgressBar) arg0.findViewById(R.id.itemhome_count_Pro);
			count_tx = (TextView) arg0.findViewById(R.id.itemhome_count_tx);
			left_tx = (TextView) arg0.findViewById(R.id.itemhome_left_tx);
			total_tx = (TextView) arg0.findViewById(R.id.itemhome_total_tx);
			shop_imgbt = (ImageButton) arg0.findViewById(R.id.itemhome_shop_imgbt);
			cart_imgbt = (ImageButton) arg0.findViewById(R.id.itemhome_cart_imgbt);
		}

	}

	@Override
	public int getItemCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public MyViewHoler onCreateViewHolder(ViewGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		MyViewHoler holer = new MyViewHoler(inflater.inflate(R.layout.item_limshop, arg0, false));
		return holer;
	}

	@Override
	public void onBindViewHolder(final MyViewHoler holer, int arg1) {
		// TODO Auto-generated method stub
		int price = TransferTool.Strtoint(list.get(arg1).getGoods_price(), ".");
		int count = list.get(arg1).getSurplus();
		Double pro = (double) ((price - count) / (price / 100));
		holer.price_tx.setText(list.get(arg1).getGoods_price());
		holer.total_tx.setText(TransferTool.toString(price));
		holer.count_tx.setText(TransferTool.toString(price - count));
		holer.left_tx.setText(TransferTool.toString(count));
		holer.count_Pro.setProgress(pro.intValue());
		BitmapUtils utils = new BitmapUtils(context);
		utils.display(holer.image_img, Values.BASE_IMAGE_URL + list.get(arg1).getCover_img().get(0));
		holer.cart_imgbt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (holderListener != null) {
					int[] start_location = new int[2];
					holer.image_img.getLocationInWindow(start_location);// 获取点击商品图片的位置
					Drawable drawable = holer.image_img.getDrawable();// 复制一个新的商品图标
					// mHolderClickListener.onHolderClick(drawable,start_location);
					holderListener.SetOnClinck(drawable, start_location);
				}
			}
		});
	}

	public void SetViewHolderListener(SetHolderListener holderListener) {
		this.holderListener = holderListener;
	}

	public interface SetHolderListener {
		void SetOnClinck(Drawable drawable, int[] start_location);
	}

}
